package com.train.controller;

import com.train.dto.AttDTO;
import com.train.dto.EmpAttDTO;
import com.train.dto.EmployeeDTO;
import com.train.mapper.AttMapper;
import com.train.mapper.EmployeeMapper;
import com.train.pojo.Attendence;
import com.train.pojo.Department;
import com.train.service.DepartmentService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.Collection;

@Controller
public class AttController {
    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private AttMapper attMapper;

    @GetMapping("att")
    public String list(@ModelAttribute("msg")String msg, @ModelAttribute("msg")String msgCode, Model model){
        Collection<AttDTO> attDTOS=attMapper.selectAllAttDTO();
        model.addAttribute("atts",attDTOS);
        return "att/attList.html";
    }

    @GetMapping("/delAtt/{id}")
    public String delAtt(@PathVariable("id")int id, RedirectAttributes rettr){
        attMapper.delAtt(id);
        rettr.addFlashAttribute("" +
                "msg","记录删除成功");
        rettr.addFlashAttribute("msgCode","200");
        return "redirect:/att";
    }

    @GetMapping("/empAtt")
    public String empAtt(Model model){
        Collection<Department> departments = departmentService.selectAllDepartment();
        model.addAttribute("departments",departments);
        return "att/findEmpAtt.html";
    }

    @PostMapping("/findEmpAtt")
    public String findEmpAtt(@RequestParam(value = "employeename",required = false) String employeename,
                             @RequestParam(value = "departmentname",required = false)String departmentname,
                             Model model){
        EmpAttDTO empAttDTO=new EmpAttDTO();
        if (employeename!=null && employeename!=""){
            empAttDTO.setEmployeename(employeename);
        }
        if (departmentname!=null && departmentname!=""){
            empAttDTO.setDepartmentname(departmentname);
        }
        Collection<EmpAttDTO> empAttDTOS=attMapper.selectAllEmpAtt(empAttDTO);
        Collection<EmpAttDTO> empAttDTOS1=attMapper.selectEmpAttSum(empAttDTO);
        Collection<Department> departments = departmentService.selectAllDepartment();
        model.addAttribute("empAttDTO",empAttDTOS);
        model.addAttribute("empAttDTO1",empAttDTOS1);
        model.addAttribute("departments",departments);
        return "att/findEmpAtt.html";
    }

    @PostMapping("/findDepAtt")
    public String findDepAtt(@RequestParam(value = "courseid",required = false)Integer courseid,
                             Model model){
        Collection<EmployeeDTO> employeeDTOS=employeeMapper.selectAllEmployeeDTOByCourseid(courseid);
        Collection<Department> departments = departmentService.selectAllDepartment();
        if (employeeDTOS.isEmpty()){
            model.addAttribute("msg","未找到考勤记录");
            model.addAttribute("msgCode","400");
        }
        model.addAttribute("emps",employeeDTOS);
        model.addAttribute("departments",departments);
        return "att/findDepAtt.html";
    }

    @GetMapping("/depAtt")
    public String depAtt(Model model){
        Collection<Department> departments = departmentService.selectAllDepartment();
        model.addAttribute("departments",departments);
        return "att/findDepAtt.html";
    }

    @GetMapping("/train")
    public String train(@ModelAttribute("msg")String msg, @ModelAttribute("msg")String msgCode, Model model){
        System.out.println(msg);
        System.out.println(msgCode);
        Collection<Department> departments = departmentService.selectAllDepartment();
        model.addAttribute("departments",departments);
        return "train.html";
    }

    @PostMapping("AddAtt")
    public String addAtt(Attendence attendence,RedirectAttributes rettr){
        if (attMapper.selectTime(attendence.getCourseid())==1){
            if (attMapper.selectIsExist(attendence).isEmpty()){
                attMapper.AddAtt(attendence);
                rettr.addFlashAttribute("" + "msg","打卡成功！");
                rettr.addFlashAttribute("msgCode","200");
                return "redirect:/train";
            }
            else{
                rettr.addFlashAttribute("" + "msg","不能重复打卡！");
                rettr.addFlashAttribute("msgCode","400");
                return "redirect:/train";
            }
        }
        else {
            rettr.addFlashAttribute("" + "msg","不在时间范围内！");
            rettr.addFlashAttribute("msgCode","400");
            return "redirect:/train";
        }


    }
}
